const AV = require("leanengine");
const utils = require("./utils");
const ip = utils.getIpAdress();
let env = "prod";
if (ip === "172.27.0.13" || env === "prod") {
  console.log("生产环境");
  // 生产
  AV.init({
    appId: "w5i4RpNBvSFQLIk46GkhXd3T-gzGzoHsz",
    appKey: "KNBV61pp2YX8hK4FuK37v6GW",
    masterKey: "kCNTvFLxm5gobib15pVAm0vB"
  });
} else {
  console.log("测试环境");
  AV.init({
    appId: "TtxW25v5pffnKPBw7A5Sioxv-gzGzoHsz",
    appKey: "F36hkyj4GgOUWnQUCymj7K9S",
    masterKey: "P3gmQqJiuyimjfbqBlTsGWDf"
  });
}

// 如果不希望使用 masterKey 权限，可以将下面一行删除
AV.Cloud.useMasterKey();

const app = require("./app");

// 端口一定要从环境变量 `LEANCLOUD_APP_PORT` 中获取。
// LeanEngine 运行时会分配端口并赋值到该变量。
const PORT = parseInt(
  process.env.LEANCLOUD_APP_PORT || process.env.PORT || 3000
);
app.listen(3000, function(error) {
  console.log("Node app is running on port:", `http://localhost:${PORT}`);

  // 注册全局未捕获异常处理器
  process.on("uncaughtException", function(error) {
    console.error("Caught exception:", error.stack);
  });
  process.on("unhandledRejection", function(reason, p) {
    console.error(
      "Unhandled Rejection at: Promise ",
      p,
      " reason: ",
      reason.stack
    );
  });
});
